/**
 * $Id$
 * @author mcanovas
 * @date   Sep 8, 2011 11:13:55 AM
 *
 * Copyright (C) 2011 Scytl Secure Electronic Voting SA
 *
 * All rights reserved.
 *
 */
package org.sk.gsync.app.synch;

public class DefaultSynchItem<T> {

    private DefaultSynchItem<T> parent;

    private T t;

    private Integer level;

    private String title;

    private Integer revision;

    public T getEmbeddedObject() {
        return t;
    }

    public void setEmbeddedObject(final T t) {
        this.t = t;
    }

    /**
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(final Object obj) {
        boolean equals = false;
        @SuppressWarnings("unchecked")
        DefaultSynchItem<T> item = (DefaultSynchItem<T>) obj;
        Integer itemLevel = item.getLevel();
        if (itemLevel != null && getLevel() != null
            && getLevel().equals(itemLevel)) {
            equals = true;
        } else {
            equals = false;
        }
        String itemTitle = item.getTitle();
        if (!itemTitle.isEmpty() && !getTitle().isEmpty()
            && itemTitle.equals(getTitle())) {
            equals = true;
        } else {
            equals = false;
        }
        return equals;
    }

    /**
     * @return Returns the level.
     */
    public Integer getLevel() {
        return level;
    }

    /**
     * @param level
     *            The level to set.
     */
    public void setLevel(final Integer level) {
        this.level = level;
    }

    /**
     * @return Returns the parent.
     */
    public DefaultSynchItem getParent() {
        return parent;
    }

    /**
     * @param parent
     *            The parent to set.
     */
    public void setParent(final DefaultSynchItem parent) {
        this.parent = parent;
    }

    public boolean isPlaced() {
        return (level != null && parent != null);
    }

    /**
     * @return Returns the title.
     */
    public String getTitle() {
        return title;
    }

    /**
     * @param title
     *            The title to set.
     */
    public void setTitle(final String title) {
        this.title = title;
    }

    /**
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        if (t != null) {
            if (level != null && level == 0) {
                sb.append("+");
            } else {
                sb.append("\\");

            }
            sb.append(getTitle());
            if (level != null && level == 0) {
                sb.append("\\");
            }
        }
        return sb.toString();
    }

    /**
     * @return Returns the revision.
     */
    public Integer getRevision() {
        return revision;
    }

    /**
     * @param revision The revision to set.
     */
    public void setRevision(Integer revision) {
        this.revision = revision;
    }

}
